$(function() {
	var bussinessId = $("#identify").val();

	initPage(bussinessId);

});
// 展示详细信息
$("body").on("click",".hrefClick",function() {
	$("#inspectEnvir").text("");
	// 初始化面板信息
	var inspectId = $(this).attr("defineId");// 自定义的属性,方便id的传递

	$.ajax({
			url : window.rootPath
					+ '/ui/bussinessInspection/findById/'
					+ inspectId,
		
			dataType : 'json',
			type : 'get',
			success : function(result) {
				if (result.code == 1) {
					var info = result.data.info;
					// 构建面板数据
					$("#bussinessName")
							.text(
									info.businesssellerBaseInfo.shopName);
					$("#bussiessAddr")
							.text(
									info.businesssellerBaseInfo.shopAdress);
					$("#bussinessMng")
							.text(
									info.businesssellerBaseInfo.managementPerson.name);
					$("#remark").text(info.remark);
					var list=info.environmentOpt;
					var enr='';
					for(var i=0;i<list.length;i++){
						for(var j=i+1;j<list.length;j++){
							if(list[i].group==list[j].group){
								list[i].group='';
							}
						}
						if(list[i].group!=''){
							enr+=list[i].group+"<br/> ";
						}
					}
					$("#inspectEnvir").append(enr);
					// $("#inspectEnvirResult").text(info.environmentOpt.title);
					$("#inspectCondition").text(
							info.inspectCondition.title);
					
					$("#inspectDate").text(moment(info.inspectDate).format('YYYY-MM-DD HH:mm'));
					$("#inspectPerson").text(info.user.name);
				}
			}
		});
	// 指向面板属性的id
	$(this).prop("href", "#detailInspectPanel");

});
function initPage(bussinessId) {
	// $("#inspectUl").empty();
	$.ajax({
		url : window.rootPath
				+ '/ui/bussinessInspection/pageOnPhone?device=mobile&identify='
				+ bussinessId,
		dataType : 'json',
		type : 'get',
		success : function(result) {
			var uncheckedInfo = result.data.uncheckedInfo;
			var checkedInfo = result.data.checkedInfo;

			if (result.code == 1) {
				// 构建巡检记录
				// buildUncheckedInspection(uncheckedInfo);
				// 构建巡检历史
				// buildUncheckedInspection(checkedInfo);

				buildInfo(uncheckedInfo, "#unCheckedInspections");
				buildInfo(checkedInfo, "#checkedInspections");

			}
		}
	});
};

function buildInfo(info, str) {

	if (info.length == 0) {
		var contenthtml = '<li><h2 style="color:blue">暂无巡检信息</h2></li>';
		$(str).append(contenthtml);
		$(str).listview('refresh');
	}

	for (var i = 0; i < info.length; i++) {

		var purl = info[i].pictureAddr;// 没有上传图片

		if (purl == undefined || purl == "") {
			purl = "img/nopicture.png";
		}
		purl=rootPath+"/"+purl;
		var title = info[i].inspectCondition.title;
		var contenthtml = '<li><a defineId=' + info[i].id
				+ ' class="hrefClick"><img width="80%" src="' +purl
				+ '" /><h2 >总体评价:' + title + '</h2>'
				+ '<p style="color:blue">时间：'
				+ moment(info[i].inspectDate).format('YYYY-MM-DD HH:mm') + '</p>'
				+ '</a>' + '</li>';

		$(str).append(contenthtml);
	}
	$(str).listview('refresh');
};
$(document).ready(
		function() {
			$("#goHome").attr(
					"href",
					$("#goHome").attr("href") + "&&identify="
							+ $('#identify').val());
		});
// 删除该条巡检,使用的是restful风格的DELETE提交方式
function deleteInspection(data) {

	if (confirm('确定删除该条巡检记录?')) {
		$
				.ajax({
					url : window.rootPath + '/ui/bussinessInspection/delete/'
							+ data.id,
					dataType : 'json',
					type : 'get',
					data : '_method=DELETE',
					success : function(result) {
						// 返回成功gg
						if (result.code == 1) {
							// 放回商家的巡检页面
							window.location = window.rootPath
									+ '/ui/bussinessInspection?device=mobile&identify='
									+ data.businesssellerBaseInfo.id;
						}
					}
				});
	}
};

// 修改页面
function editInspection(data) {
	if (confirm('确定修改该条巡检记录?')) {
		// 修改页面
		window.location = window.rootPath
				+ "/ui/bussinessInspection/saveOrUpdate/" + data.id
				+ "?device=mobile&identify=" + data.businesssellerBaseInfo.id;
	}
};

$("#saveInspectBtn").click(function() {

	window.location = window.rootPath
			+ "/ui/bussinessInspection/saveOrUpdate?device=mobile&identify="
			+ $("#identify").val();
	
	
});
